"""Encapsulates action request."""
import logging
log = logging.getLogger("ECC")


class ActionRequest(object):
    """A wrapper for action request.

    Provides a way to identify an action request and provide some information
    used when creating the request.
    """

    def __init__(self, view, trigger_position):
        """Initialize the object.

        Args:
            view (sublime.View): The view for which request is created.
            trigger_position(int): The position for which request was created.
        """
        self._view = view
        self._trigger_position = trigger_position

    def get_view(self):
        """Return the view for which action was requested."""
        return self._view

    def get_trigger_position(self):
        """Get position of the trigger for which action was requested."""
        return self._trigger_position

    def get_identifier(self):
        """Generate unique tuple for file and trigger position."""
        return (self._view.buffer_id(), self._trigger_position)

    def is_suitable_for_view(self, view):
        """Check if view is suitable for this action request.

        Return True if specified view and its current position is deemed
        suitable for completions generated by this action request. """
        if view != self._view:
            log.debug("Active view doesn't match action view")
            return False
        # We accept both current position and position to the left of the
        # current word as valid as we don't know how much user already typed
        # after the trigger.
        current_position = view.sel()[0].a
        valid_positions = [current_position, view.word(current_position).a]
        if self._trigger_position not in valid_positions:
            log.debug("View's trigger positions %s doesn't match action "
                      "trigger position %s",
                      valid_positions,
                      self._trigger_position)
            return False
        return True
